package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mapper.GoodsMapper;
import com.mapper.UserMapper;
import com.pojo.dto.AdminAddGoodDto;
import com.pojo.dto.AdminUpdateDto;
import com.pojo.entity.Goods;
import com.pojo.entity.User;
import com.service.IAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 杨世博
 * @date: Created in 15:36 2023/6/9
 */
@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public IPage<Goods> addGoods(AdminAddGoodDto adminAddGoodDto) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(adminAddGoodDto,goods);

        goodsMapper.insert(goods);
        return getGoods();
    }

    @Override
    public IPage<Goods> updateGoods(Goods good) {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("goods_id", good.getGoodsId());
        goodsMapper.update(good, goodsQueryWrapper);

        return getGoods();
    }

    @Override
    public IPage<Goods> deleteGoods(Integer goodsId) {
        QueryWrapper<Goods> gqw = new QueryWrapper<>();
        gqw.eq("goods_id", goodsId);
        goodsMapper.delete(gqw);
        return getGoods();
    }

    @Override
    public Boolean goodsStatus(Integer goodsId, Boolean goodsStatus) {
        goodsMapper.updateStatus(goodsId, goodsStatus);
        return true;
    }

    @Override
    public IPage<User> getUser(String key, Integer pageNum, Integer size) {

        IPage<User> iPage = new Page<>();

        List<User> users = userMapper.selectAllByAdmin(key, (pageNum-1) * size, size);

        iPage.setPages(pageNum);
        iPage.setSize(size);
        iPage.setRecords(users);
        iPage.setCurrent(userMapper.selectCountByAdmin(key));

        return iPage;
    }

    @Override
    public IPage<User> updateUser(AdminUpdateDto adminUpdateDto) {
        User user = new User();
        BeanUtils.copyProperties(adminUpdateDto, user);
        QueryWrapper<User> uqw = new QueryWrapper<>();
        uqw.eq("uid",adminUpdateDto.getUid());
        userMapper.update(user,uqw);
        return getUser();
    }

    @Override
    public IPage<User> deleteUser(Integer uid) {
        userMapper.deleteById(uid);
        return getUser();
    }

    @Override
    public IPage<Goods> getGoods(String key, Integer pageNum, Integer size) {
        IPage<Goods> iPage = new Page<>();

        List<Goods> goods = goodsMapper.selectGoodsByAdmin(key,(pageNum-1) * size, size);

        iPage.setPages(pageNum);
        iPage.setSize(size);
        iPage.setRecords(goods);
        iPage.setCurrent(goodsMapper.selectCountByAdminKey(key));

        return iPage;
    }

    public IPage<User> getUser(){
        IPage<User> iPage = new Page<>();
        Integer pageNum = 1;
        Integer size = 10;
        List<User> users = userMapper.selectUserByAdmin((pageNum-1) * size, size);

        iPage.setPages(pageNum);
        iPage.setSize(size);
        iPage.setRecords(users);
        iPage.setCurrent(userMapper.selectUserCountByAdmin());

        return iPage;
    }


    private IPage<Goods>  getGoods(){
        IPage<Goods> iPage = new Page<>();

        Integer size = 10;
        Integer pageNum = 1;

        List<Goods> goods = goodsMapper.selectAllByAdmin((pageNum-1) * size, size);

        iPage.setPages(pageNum);
        iPage.setSize(size);
        iPage.setRecords(goods);
        iPage.setCurrent(goodsMapper.selectCountByAdmin());

        return iPage;
    }
}
